home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / Stopwatch2.3 / Source / ClientViewMgr.m < prev    next >
Text File  |  1995-06-12  |  6KB  |  257 lines

  1. /*
  2.  * For legal stuff see the file COPYRIGHT
  3.  */
  4. #import "ClientInspector.h"
  5. #import "ClientViewMgr.h"
  6.  
  7. /* Indices of the slots in the form */
  8. typedef enum {
  9.   ClientSlot,
  10.   ShortNameSlot,
  11.   StreetSlot,
  12.   CitySlot,
  13.   StateSlot,
  14.   ZipSlot,
  15.   ContactSlot,
  16.   EmailSlot,
  17.   FaxSlot,
  18.   RateSlot
  19. } FormField;
  20.  
  21. @interface ClientViewMgr(PRIVATE)
  22. - showInfo:(ClientInfo *)info inspector:(ClientInspector *)inspector;
  23. - (void)loadFromForm:(ClientInfo *)info;
  24. - clearForm;
  25. - setIsEditing:(BOOL)flag;
  26. @end
  27.  
  28. @implementation ClientViewMgr
  29.  
  30. - awakeFromNib
  31. {
  32.   [dataForm setTextDelegate:self];
  33.   return self;
  34. }
  35.  
  36. - mgrShow:(ClientInspector *)inspector
  37. {
  38.   [self showInfo:[inspector selectedClient] inspector:inspector];
  39.   return self;
  40. }
  41.  
  42. /*
  43.  * This view ignores double clicks from the main client browser.
  44.  */
  45. - (BOOL)mgrDoubleClick:(ClientInspector *)inspector
  46. {
  47.   return NO;
  48. }
  49.  
  50. /*
  51.  * Create a new ClientInfo object and copy the data from the form.
  52.  */
  53. - (BOOL)mgrAdd:(ClientInspector *)inspector
  54. {
  55.   ClientInfo *info = [[ClientInfo alloc] init];
  56.  
  57.   [self loadFromForm:info];
  58.   [inspector addClient:info];
  59.   [self showInfo:info inspector:inspector];
  60.   return YES;
  61. }
  62.  
  63. /*
  64.  * Delete the ClientInfo object from the list.
  65.  */
  66. - (BOOL)mgrDelete:(ClientInspector *)inspector
  67. {
  68.   ClientInfo *info;
  69.  
  70.   if ( NXRunAlertPanel( [NXApp appName], "Delete client `%s'?",
  71.            "Yeah. Who needs 'em", "Perhaps not", NULL,
  72.                [[inspector selectedClient] shortName] ) != NX_ALERTDEFAULT ) {
  73.     return NO;
  74.   }
  75.  
  76.   info = [inspector deleteSelectedClient];
  77.   [self saveDeletedItem:info];
  78.   [self showInfo:[inspector selectedClient] inspector:inspector];
  79.   return YES;
  80. }
  81.  
  82. - (BOOL)mgrUndelete:(ClientInspector *)inspector
  83. {
  84.   ClientInfo *info = [self getDeletedItem];
  85.  
  86.   if ( info ) {
  87.     [inspector addClient:info];
  88.     [self showInfo:info inspector:inspector];
  89.     return YES;
  90.   }
  91.  
  92.   NXBeep();
  93.   return NO;            /* indicate that no save is required */
  94. }
  95.  
  96. /*
  97.  * Modify the ClientInfo object for the currently selected client.
  98.  */
  99. - (BOOL)mgrModify:(ClientInspector *)inspector
  100. {
  101.   ClientInfo *info = [inspector selectedClient];
  102.  
  103.   [self loadFromForm:info];
  104.   [inspector decacheBrowser];
  105.   [[NXApp delegate] decacheBrowser];
  106.   [self showInfo:info inspector:inspector];
  107.   return YES;
  108. }
  109.  
  110. - (BOOL)canAdd
  111. {
  112.   return enteringNewClient;
  113. }
  114.  
  115. - (BOOL)canModify
  116. {
  117.   return hasSelection && isEditing;
  118. }
  119.  
  120. - (BOOL)canDelete
  121. {
  122.   return hasSelection;
  123. }
  124.  
  125. - (BOOL)isEditing
  126. {
  127.   return isEditing;
  128. }
  129.  
  130.  
  131. @implementation ClientViewMgr(PRIVATE)
  132.  
  133. - showInfo:(ClientInfo *)info inspector:(ClientInspector *)inspector
  134. {
  135.   if ( info == nil ) {
  136.     [self clearForm];
  137.   } else {
  138.     [dataForm setStringValue:[info contactName] at:ContactSlot];
  139.     [dataForm setStringValue:[info clientName]  at:ClientSlot];
  140.     [dataForm setStringValue:[info shortName]   at:ShortNameSlot];
  141.     [dataForm setStringValue:[info street]      at:StreetSlot];
  142.     [dataForm setStringValue:[info city]        at:CitySlot];
  143.     [dataForm setStringValue:[info addrState]   at:StateSlot];
  144.     [dataForm setStringValue:[info zipCode]     at:ZipSlot];
  145.     [dataForm setStringValue:[info emailAddr]   at:EmailSlot];
  146.     [dataForm setStringValue:[info faxNumber]   at:FaxSlot];
  147.     [dataForm setFloatValue: [info hourlyRate]  at:RateSlot];
  148.     hasSelection = YES;
  149.   }
  150.   [dataForm selectTextAt:ClientSlot];
  151.   [self setIsEditing:NO];
  152.   return self;
  153. }
  154.  
  155. /*
  156.  * Copy data from the form into the given ClientInfo object
  157.  */
  158. - (void)loadFromForm:(ClientInfo *)info
  159. {
  160.   [info setContactName: [dataForm stringValueAt:ContactSlot]];
  161.   [info setClientName:  [dataForm stringValueAt:ClientSlot]];
  162.   [info setShortName:   [dataForm stringValueAt:ShortNameSlot]];
  163.   [info setStreet:      [dataForm stringValueAt:StreetSlot]];
  164.   [info setCity:        [dataForm stringValueAt:CitySlot]];
  165.   [info setAddrState:   [dataForm stringValueAt:StateSlot]];
  166.   [info setZipCode:     [dataForm stringValueAt:ZipSlot]];
  167.   [info setEmailAddr:   [dataForm stringValueAt:EmailSlot]];
  168.   [info setFaxNumber:   [dataForm stringValueAt:FaxSlot]];
  169.   [info setHourlyRate:  [dataForm floatValueAt:RateSlot]];
  170. }
  171.  
  172. - clearForm
  173. {
  174.   [dataForm setStringValue:"" at:ContactSlot];
  175.   [dataForm setStringValue:"" at:ClientSlot];
  176.   [dataForm setStringValue:"" at:ShortNameSlot];
  177.   [dataForm setStringValue:"" at:StreetSlot];
  178.   [dataForm setStringValue:"" at:CitySlot];
  179.   [dataForm setStringValue:"" at:StateSlot];
  180.   [dataForm setStringValue:"" at:ZipSlot];
  181.   [dataForm setStringValue:"" at:EmailSlot];
  182.   [dataForm setStringValue:"" at:FaxSlot];
  183.   [dataForm setStringValue:"" at:RateSlot];
  184.  
  185.   [dataForm selectTextAt:ContactSlot];
  186.   hasSelection = NO;
  187.   return self;
  188. }
  189.  
  190. - setIsEditing:(BOOL)flag
  191. {
  192.   isEditing = flag;
  193.  
  194.   if ( isEditing == NO )
  195.     enteringNewClient = NO;    /* can't be entering a new one if not editing! */
  196.     
  197.   [[ClientInspector sharedInstance] setDocEdited:flag];
  198.   return self;
  199. }
  200.  
  201. /*
  202.  * Delegated from Form. If the user starts typing over an existing
  203.  * entry, enable the Update and Cancel buttons. Disable delete too,
  204.  * forcing the user to use the delete button only after selecting
  205.  * an entry.
  206.  */
  207. - textDidChange:textObj
  208. {
  209.   if ( enteringNewClient == NO && [dataForm selectedIndex] == ClientSlot ) {
  210.     enteringNewClient = YES;
  211.     [self setIsEditing:YES];
  212.   } else if ( isEditing == NO )
  213.     [self setIsEditing:YES];
  214.   else
  215.     return self;        /* do nothing */
  216.  
  217.   [ClientInspector updateButtons];
  218.   return self;
  219. }
  220.  
  221. /*
  222.  * Allow pasting in of client data
  223.  */
  224. - paste:sender
  225. {
  226.   id pb=[Pasteboard newName:NXGeneralPboard];
  227.   const char *pTypes[2] = { NXAsciiPboardType, NULL};
  228.     
  229.   if ( [pb findAvailableTypeFrom:pTypes num:1] ) {
  230.     NXStream *stream;
  231.     int cols, rows, i, r;
  232.     char line[1024];
  233.         
  234.     stream=[pb readTypeToStream:NXAsciiPboardType];
  235.         
  236.     r=0;
  237.     [dataForm getNumRows:&rows numCols:&cols];
  238.     for (i=0; i < rows && r != EOF; i++) {
  239.       r=NXScanf(stream, "%[^\n\000]", line);
  240.       [dataForm setStringValue:line at:i];
  241.       if (r != EOF ) {
  242.     r=NXGetc(stream);
  243.     r=NXGetc(stream);
  244.     if (r != EOF ) NXUngetc(stream);
  245.       }
  246.     }
  247.     NXCloseMemory(stream, NX_FREEBUFFER);
  248.     [ClientInspector updateButtons];
  249.             
  250.     return self;
  251.   }
  252.  
  253.   return nil;
  254. }
  255.  
  256. @end
  257.